package cn.yyx.empservice.controller;


import cn.yyx.common.R;
import cn.yyx.empservice.entity.employee.EmpQuery;
import cn.yyx.empservice.entity.employee.Employee;
import cn.yyx.empservice.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sun
 * @since 2023-10-25
 */
@RestController
@RequestMapping("/empservice")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 模糊分页查询
     * @param cp
     * @param ls
     * @return
     */
    @PostMapping("list/{cp}/{ls}")
    public R getemployeeList(@PathVariable Integer cp, @PathVariable Integer ls, @RequestBody EmpQuery query){
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(query.getName()))
            queryWrapper.eq("name",query.getName());
        Page<Employee> page = new Page<>(cp, ls);
        employeeService.page(page,queryWrapper);
        return R.ok().data("records",page.getRecords()).data("total",page.getTotal());
    }

    @PostMapping("listForAdd")
    public R getemployeeListForAdd(){
        return R.ok().data("records",employeeService.list());
    }

    /**
     * 根据编号查询员工信息
     * @param id
     * @return
     */
    @GetMapping("preUpdate/{id}")
    public R getemployeeInfo(@PathVariable Long id){
        System.out.println(employeeService.getById(id));
        return R.ok().data("employee",employeeService.getById(id));
    }

    /**
     * 根据编号修改员工信息
     * @param employee 保存需要修改的员工信息的实体
     * @return
     */
    @PutMapping("update")
    public R updateemployeeInfo(@RequestBody Employee employee){
        return employeeService.updateById(employee)?R.ok():R.error();
    }

    /**
     * 根据员工编号删除员工
     * @param id 员工编号
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteemployeeId(@PathVariable String id){
        return employeeService.removeById(id)?R.ok():R.error();
    }


    @PostMapping("addemployee")
    public R addemployee(@RequestBody Employee employee){
        return employeeService.save(employee)?R.ok():R.error();
    }



}

